#!/usr/bin/perl

# autolatex - gnuplot2eps
# Copyright (C) 1998-07  Stephane Galland <galland@arakhne.org>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; see the file COPYING.  If not, write to
# the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
# Boston, MA 02111-1307, USA.

use strict;

use File::Temp qw/ :POSIX /;
use File::Copy;
use File::Spec;
use File::Basename;

my $INPUTFILE = $ARGV[0];

my $tmpfilename = File::Spec->catfile(dirname("$INPUTFILE"),"plottmp.plot");

local *OF;
local *IF;

open(*OF,"> $tmpfilename") or die("output $tmpfilename: $!\n");
open(*IF,"< $INPUTFILE") or die("input $INPUTFILE: $!\n");

print OF "set terminal postscript eps color\n";

while (my $line = <IF>) {
  if ($line !~ /^\s*quit\s*$/) {
    print OF "$line";
  }
}

print OF "quit\n";

close(*IF);
close(*OF);

my $dir = dirname($tmpfilename);
chdir("$dir") or die(dirname("$tmpfilename").": $!\n");

my @CMD = ("gnuplot",
	   basename("$tmpfilename"),
	   );

system(@CMD);

$dir =~ s/[^\/]+/../g;
chdir("$dir");
unlink "$tmpfilename";

exit(0);
